package com.Final.hj;


import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button loginBtn, toRegister, loginFPaw;
    private Toolbar mToolbar;



    //文本框
    private EditText uname;
    private EditText paw;

    private String un;
    private String pa;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        setSupportActionBar(mToolbar);
    }


    public void initView(){
        loginBtn = (Button) findViewById(R.id.login_btn);
        toRegister = (Button) findViewById(R.id.login_to_register);
        loginFPaw = (Button) findViewById(R.id.login_forget_paw);

        //文本框
        uname = (EditText) findViewById(R.id.login_edit_uname);
        paw = (EditText) findViewById(R.id.login_edit_paw);

        //标题栏
        mToolbar = (Toolbar) findViewById(R.id.toolBar);

        loginBtn.setOnClickListener(this);
        toRegister.setOnClickListener(this);
        loginFPaw.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.login_btn:
                sendLoginHttp();
                break;
            case R.id.login_to_register:
                //跳转注册页面
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
                //把当前页面销毁掉
                MainActivity.this.finish();
                break;
            case R.id.login_forget_paw:
                //找回密码
                lookPaw();
                break;
        }
    }


    public void lookPaw(){
        // cd data/data/com.Final.hj/databases
//        Intent intent = new Intent(MainActivity.this, TaskJkes.class);
        Intent intent = new Intent(MainActivity.this, StudenSystem.class);
        startActivity(intent);
    }

    //发送get请求
    public void sendLoginHttp(){
        //处理数据判断用户填写数据是否正确
        un =  uname.getText().toString().trim();
        pa =  paw.getText().toString().trim();

        if(TextUtils.isEmpty(un)){
            Toast.makeText(this, "用户不能为空", Toast.LENGTH_LONG).show();
            return;
        }

        if(TextUtils.isEmpty(pa)){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();

                    Request request = new Request.Builder()
                            .url("http://192.168.43.124:8080/login?uname="+ un + "&paw="+ pa)
                            .build();

                    Response response = client.newCall(request).execute();
                    String string = response.body().string();


                    System.out.println("已经得到数据");
                    //调用函数解析json
                    login(string);
                }catch (Exception e){
                    e.printStackTrace();
                    System.out.println("2222");

                }
            }
        }).start();
    }


    public void login(String str){
            if(str.equals("0")){
                //在子线程 中不能使用Toast 必须增加下面这两行
                Looper.prepare();
                Toast.makeText(this, "用户名错误", Toast.LENGTH_LONG).show();
                Looper.loop();
                return;
            }else if(str.equals("-1")){

                Looper.prepare();
                Toast.makeText(this, "密码错误", Toast.LENGTH_LONG).show();
                Looper.loop();
                return;
            }

        String[] data = str.split(",_");
        LoginSQLManger.getInstance(MainActivity.this).deleteAll();
        SQLSavaInfo info = new SQLSavaInfo(un, data[0], data[1], data[2], data[3], data[4], data[5]);
        LoginSQLManger.getInstance(MainActivity.this).insertInfo(info);
            //跳转到首页
        Intent intent = new Intent(MainActivity.this, HomeActivity.class);

        startActivity(intent);

        //销毁当前页面
        MainActivity.this.finish();
    }
}
